package com.example.mall.controller;

import com.example.mall.pojo.*;
import com.example.mall.service.SpOrderGoodsService;
import com.example.mall.service.SpOrderService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@AllArgsConstructor
public class OrdersController {
    SpOrderService spOrderService;
    SpOrderGoodsService spOrderGoodsService;


    //获取订单列表
    @GetMapping("/orders")
    public Rbody getOrderByQuery(@RequestParam("query") String query,@RequestParam("pagenum") Integer pagenum,@RequestParam("pagesize") Integer pagesize) {
        Rbody rbody = new Rbody();
//        System.out.println("query："+query);
////        System.out.println("controller pagenum:"+pagenum);
////        System.out.println("controller"+pagesize);

        List<SpOrder> spOrders = spOrderService.getSpOrderByQuery(query, pagenum, pagesize);
        PageInfo<SpOrder> info = new PageInfo<>(spOrders, pagesize);
////        获取data
        OrderPage orderPage = new OrderPage();
        orderPage.setTotal((int) info.getTotal());
        orderPage.setPagenum(info.getPageNum());
        orderPage.setGoods(info.getList());

        rbody.setData(orderPage);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("获取订单列表成功！");
        return rbody;
    }


    //修改订单状态
    @PutMapping("/orders/{id}")
    public Rbody getListId(@PathVariable("id") Integer order_id,  @RequestBody ApOrder spO){
        Rbody rbody = new Rbody();

        //请求参数
        SpOrder spOrder = new SpOrder();
        spOrder.setOrder_id(order_id);
        spOrder.setIs_send(spO.getIs_send());
        spOrder.setOrder_pay(spO.getOrder_pay());
        spOrder.setOrder_price(spO.getOrder_price());
        spOrder.setOrder_number(spO.getOrder_number());
        spOrder.setPay_status(spO.getPay_status());

        System.out.println(order_id);

        //响应数据
        OrderUpdateData orderUpdateData = spOrderService.updateOrder(spOrder);
        if (orderUpdateData.getOrder_id()==null&&orderUpdateData.getOrder_id().equals("")){
            //错误
            rbody.getMeta().setStatus(500);
            rbody.getMeta().setMsg("内部错误");
        }else{
            //正确
            rbody.getMeta().setStatus(200);
            rbody.getMeta().setMsg("获取成功");
            rbody.setData(orderUpdateData);
        }
        return rbody;
    }

    //查看订单详情
    @GetMapping("/orders/{id}")
    public Rbody getOrderById(@PathVariable("id") Integer order_id){
        Rbody rbody = new Rbody();
        //根据order_id获取信息
//        List<SpOrder> spOrders=spOrderService.getSpOrderById(order_id);
        SpOrder spOrders=spOrderService.getSpOrderById(order_id);

        OrderOut orderOut = new OrderOut();
        orderOut.setOrder_id(spOrders.getOrder_id());
        orderOut.setUser_id(spOrders.getUser_id());
        orderOut.setOrder_number(spOrders.getOrder_number());
        orderOut.setOrder_price(spOrders.getOrder_price());
        orderOut.setOrder_pay(spOrders.getOrder_pay());
        orderOut.setIs_send(spOrders.getIs_send());
        orderOut.setTrade_no(spOrders.getTrade_no());
        orderOut.setOrder_fapiao_title(spOrders.getOrder_fapiao_title());
        orderOut.setOrder_fapiao_company(spOrders.getOrder_fapiao_company());
        orderOut.setOrder_fapiao_content(spOrders.getOrder_fapiao_content());
        orderOut.setConsignee_addr(spOrders.getConsignee_addr());
        orderOut.setPay_status(spOrders.getPay_status());
        orderOut.setCreate_time(spOrders.getCreate_time());
        orderOut.setUpdate_time(spOrders.getUpdate_time());

        //根据order_id获取SpOrderGoods的内容
        List<SpOrderGoods> spOrderGoods=spOrderGoodsService.getSpOrderGoodsById(order_id);
        orderOut.setGoods(spOrderGoods);

        rbody.setData(orderOut);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("根据id获取订单信息成功！");
        return rbody;

    }





}
